package com.platform.api.modular.amqp.controller;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.platform.core.annotion.Permission;
import com.platform.core.pojo.response.ResponseData;
import com.platform.core.pojo.response.SuccessResponseData;
import com.rabbitmq.client.Channel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class AmqpListenerController {


    @RabbitListener(queues = "goods2CQueueTest")
    @RabbitHandler
    public void process(Message msg,Channel channel) throws IOException {
        //获取在发送消息时设置的唯一id
        MessageProperties properties = msg.getMessageProperties();
        String id = properties.getMessageId();
        log.info("=======获取到消息队列信息："+ id);
        String str = new String(msg.getBody(),"utf-8");
        long tag = properties.getDeliveryTag();
        if(StringUtils.isNotEmpty(str)){
            Map<String,Object> map = JSONObject.parseObject(str,Map.class);
            System.out.println(map.get("num")+"----"+map.get("id")+"----"+map.get("name")+"----"+map.get("phone"));
            System.out.println("----------");
            channel.basicAck(tag, false);// 手动确认消息消费者
        }else{
            channel.basicNack(tag, false, true);
        }
    }
}
